package com.wisezone.permission.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.utils.BeanUtils;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.common.web.BaseFormController;
import com.wisezone.permission.bean.entity.WzDepartmentEntity;
import com.wisezone.permission.bean.form.WzDepartmentForm;
import com.wisezone.permission.bean.search.WzDepartmentSearch;
import com.wisezone.permission.service.WzDepartmentService;

/**
 * 
 *部门; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:40 update date 2010-07-29
 *         14:44:40
 * 
 */
@WzWebAnnotation(moduleName = "dept")
public class WzDepartmentController extends
		BaseFormController<WzDepartmentEntity, WzDepartmentSearch, WzDepartmentForm> {

	/**
	  *
	  *
	  *
	  */
	private WzDepartmentService wzDepartmentService;
	
	private List<WzDepartmentForm> parentList; // 父菜单列表

	private final String TREE = "tree";
	
	private String roleIds;  //角色ID
	
	private String roleNames;  //角色名称
	
	/**
	 * 返回清空条件列表
	 * 
	 * @return
	 * @throws WzWebException
	 */
	public String toList() throws WzWebException {

		return LIST;
	}

	/**
	 * 获得列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws WzWebException {

		return LIST;
	}

	/**
	 * 进入新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterNew() throws WzWebException {

		if (id != null) {
			entity = new WzDepartmentEntity();
			entity.setParentId(id);
		}
		return NEW_EDIT;
	}

	/**
	 * 进入修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterEdit() throws WzWebException {

		if(id == null){
			id = 1L;
		}
		entity = this.wzDepartmentService.findById(id);
		return NEW_EDIT;
	}

	/**
	 * 进入树列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterTree() throws WzWebException {

		searchEntity = getSearchEntity();
		List<WzDepartmentForm> deptList = wzDepartmentService.getDepartmentList(searchEntity.getName());
		model.put("deptList", deptList);
		return TREE;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws WzWebException {

		entity = getEntity();
		entity.setState(ConstantUtils.STATE_EXIST);
		this.wzDepartmentService.save(entity);
		return toList();
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws WzWebException {

		entity = getEntity();
		WzDepartmentEntity oldEntity = this.wzDepartmentService.findById(id);
		BeanUtils.copyBean(oldEntity, entity);
		this.wzDepartmentService.update(oldEntity);
		return toList();
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws WzWebException {

		Collection<Serializable> idList = getIdList();
		if (idList != null) {
			this.wzDepartmentService.deleteAll(idList);
		}
		return toList();
	}

	public List<WzDepartmentForm> getParentList() {

		parentList = this.wzDepartmentService.getParentList();
		return parentList;
	}


	public void setWzDepartmentService(WzDepartmentService wzDepartmentService) {
		this.wzDepartmentService = wzDepartmentService;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public String getRoleNames() {
		return roleNames;
	}

	public void setRoleNames(String roleNames) {
		this.roleNames = roleNames;
	}
	

	

	

}